<template >
   <el-dialog :title="title" :close-on-click-modal="false" :before-close="closeDialog" :visible.sync="visible" width="600px" append-to-body dialogDrag>
        <el-form ref="form" :model="form" class="form" :rules="rules" size="small" label-width="100px">
            <el-form-item label="相册名称" prop="name"> 
                <el-input v-model="form.name" style="width:260px"></el-input>
                <span style="">&nbsp;&nbsp;&nbsp;&nbsp;状态：
                    <el-switch v-model="form.status" 
                        active-color="#409EFF" 
                        inactive-color="#F56C6C"
                        :active-value="1"
                        :inactive-value="0"
                        :active-text="form.status=== 1? '启用':'禁用'"
                        @change="changeEnabled(scope.row)">
                    </el-switch>
                </span>
            </el-form-item>
            <el-form-item label="相册描述">
                <el-input type="textarea" v-model="form.remark" style="width: 405px;"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="text"    @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="doSubmit" :loading="loading">确认</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { addAlbum,editAlbum,selAlbum} from '@/api/album'
import { mapGetters } from 'vuex'
export default {
    inject:['reload'],
    data() {
        return {
            rowId:'',
            title:'',
            visible: false,
            loading:false,  //确认提交按钮加载中
            form: {id:'', name:'', remark:'', status:1,userId:''},
            rules:{
                name: [{ required: true,message: '相册名称不能为空'}],
            }
        };
    },
    computed:{
        ...mapGetters(['user']),
    },
    methods: {
        closeDialog(){
            this.$refs['form'].resetFields();
            this.visible = false
            this.loading = false
            this.rowId = ''
            this.form = {id:'', name:'', remark:'', status:1}
        },
        init(row,isAdd) {
            this.title = (isAdd ? '新增相册':'编辑相册')
            !!row && (this.rowId = row.id) && this.findUserById(this.rowId)
            this.$nextTick(() => {
                this.visible = true
            })
        },
        findUserById(id){
            this.apiSelAlbumById(id)
        },
        doSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true
                    this.$confirm("确定?",'提示',{ type: 'warning'}).then( ()=>{
                        this.form.userId = this.user.id
                        if(this.rowId){
                            this.apiUpdateAlbum(this.form)
                        }else{
                            this.apiAddAlbum(this.form)
                        }
                    })
                }
            })
        },
        //==========>请求api接口<==========//
        apiSelAlbumById(id){
            selAlbum(id).then(res=>{
                if(res.code === 200){
                    this.form ={...res.data}
                }
            },error=>{
                this.$message.error(res.message)
            })
        },
        apiUpdateAlbum(album){
            editAlbum(album).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.loading =false
            })
        },
        apiAddAlbum(album){
            addAlbum(album).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.loading =false
            })
        }
        //==========>请求api接口<==========//
    },
};
</script>
<style scoped>

</style>


